﻿using Coldairarrow.Business.Base;
using Coldairarrow.Business.TD;
using Coldairarrow.Entity.TD;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Coldairarrow.IBusiness;
using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.PB;
using Coldairarrow.Entity.DTO;
using System;
using Microsoft.Extensions.Options;
using Coldairarrow.Util.ClassLibrary;

namespace Coldairarrow.Api.Controllers.TD
{
    [Route("/TD/[controller]/[action]")]
    public partial class TD_CheckController : BaseApiController
    {
        #region 获取

        [HttpPost]
        public async Task<PageResult<TD_CheckListDTO>> GetDataList(PageInput<CheckConditionDTO> input)
        {
            return await _tD_CheckBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<TD_Check> GetTheData(IdInputDTO input)
        {
            return await _tD_CheckBus.GetTheDataAsync(input.id);
        }


        /// <summary>
        /// 获取登录人所属公司的所有仓库
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<Base_Department>> GetDeptList()
        {
            return await _tD_CheckBus.GetDeptListAsync();
        }

        /// <summary>
        /// 获取仓库下的货区信息
        /// </summary>
        /// <param name="storId">仓库Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_StorArea>> GetStorAreaList(string storId)
        {
            return await _tD_CheckBus.GetStorAreaListAsync(storId);
        }

        /// <summary>
        /// 获取货区下的货架信息
        /// </summary>
        /// <param name="StorAreaId">货区Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_Rack>> GetStorRackList(string StorAreaId)
        {
            return await _tD_CheckBus.GetStorRackListAsync(StorAreaId);
        }

        /// <summary>
        /// 获取货架下的货位信息
        /// </summary>
        /// <param name="StorRackId">货架Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PB_Location>> GetStorLocaList(string StorRackId)
        {
            return await _tD_CheckBus.GetStorLocaListAsync(StorRackId);
        }

        /// <summary>
        /// 盘点选择物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<TD_CheckLocalMaterialDTO>> GetCheckLocalMaterial(PageInput<MaterialConditionDTO> input)
        {
            return await _tD_CheckBus.GetCheckLocalMaterialAsync(input);
        }

        /// <summary>
        /// 保存盘点信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveCheck(TD_CheckDTO data)
        {
            InitEntity(data);
            await _tD_CheckBus.AddCheckAsync(data);
        }

        /// <summary>
        /// 检查当前是否有备货单和收货单未完成
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<AjaxResult> GetCheckMaterialListAsync()
        {
            return await _tD_CheckBus.GetCheckMaterialListAsync();
        }

        /// <summary>
        /// 盘点信息删除
        /// </summary>
        /// <param name="ids">盘点id集合</param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteCheck(List<string> ids)
        {
            await _tD_CheckBus.DeleteCheckAsync(ids);
        }

        /// <summary>
        /// 盘点查看详细信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<TD_CheckDataDTO>> GetCheckData(PageInput<CheckConditionDTO> input)
        {
            return await _tD_CheckBus.GetCheckDataAsync(input);
        }

        /// <summary>
        /// 盘点单确定
        /// </summary>
        /// <param name="checkId">盘点单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task ToExamineCheck(string checkId)
        {
            await _tD_CheckBus.ToExamineCheckAsync(checkId);
        }

        /// <summary>
        /// 盘点单审核
        /// </summary>
        /// <param name="checkId">盘点单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task ToReviewCheck(string checkId)
        {
            await _tD_CheckBus.ToReviewCheckAsync(checkId);
        }

        /// <summary>
        /// 盘点单开始盘点
        /// </summary>
        /// <param name="checkId">盘点单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task CheckStart(string checkId)
        {
            await _tD_CheckBus.CheckStartAsync(checkId);
        }

        /// <summary>
        /// 判断当时公司下是否有盘点单
        /// 盘点单是否在开始作业
        /// 是（true）
        /// 否（false）
        /// </summary>
        /// <param name="MaterialId">物料id</param>
        /// <param name="StorId">仓库id</param>
        /// <param name="LocalId">仓库id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CheckBool> GetTrueOrFalse(string MaterialId, string StorId, string LocalId)
        {
            return await _tD_CheckBus.GetTrueOrFalse(MaterialId, StorId, LocalId);
        }

        /// <summary>
        /// 盘点完成
        /// </summary>
        /// <param name="checkId">盘点单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdateInventoryComplete(string checkId)
        {
            await _tD_CheckBus.UpdateInventoryCompleteAsync(checkId);
        }

        /// <summary>
        /// 获取盘点明细信息
        /// </summary>
        /// <param name="checkId">盘点单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TD_CheckData>> GetCheckDataList(string checkId)
        { 
           return await _tD_CheckBus.GetCheckDataListAsync(checkId);
        }

        /// <summary>
        /// 获取实际盘点信息
        /// </summary>
        /// <param name="checkId">盘点单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<TD_CheckPack>> GetCheckPack(string checkId)
        {
            return await _tD_CheckBus.GetCheckPackAsync(checkId);
        }

        /// <summary>
        /// 获取库存物料信息
        /// </summary>
        /// <param name="data">盘点明细</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<StorMaterialDTO> GetStorMaterialList(TDcheckDataDTO data)
        {
            return await _tD_CheckBus.GetStorMaterialListAsync(data);
        }

        /// <summary>
        /// 计算盘差值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<DiscDifferenceDTO> GetDiscDifference(DifferenceDTO data)
        {
            return await _tD_CheckBus.GetDiscDifferenceAsync(data);
        }

        /// <summary>
        /// 盘差
        /// </summary>
        /// <param name="Data">盘点单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpDifference(DifferenceDTO data)
        {
            await _tD_CheckBus.UpDifferenceAsync(data);
        }

        /// <summary>
        /// 盘差完成(并矫正库存数据)
        /// </summary>
        /// <param name="CheckId">盘点单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpCheckData(string CheckId)
        {
            await _tD_CheckBus.UpCheckDataAsync(CheckId);
        }

        /// <summary>
        /// PDA盘点
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task InsertCheckData(CheckData data)
        {
            await _tD_CheckBus.InsertCheckDataAsync(data);
        }

        /// <summary>
        /// PDA盘点获取包信息
        /// </summary>
        /// <param name="PackQrCode">包二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CheckPackMaterialDetailDTO> GetCheckPackMaterialDetailData(string PackQrCode)
        {
            return await _tD_CheckBus.GetCheckPackMaterialDetailDataAsync(PackQrCode);
        }

        /// <summary>
        /// PDA盘点扫描成品码输入数量
        /// </summary>
        /// <param name="JobQrCode">成品二维码</param>
        /// <param name="LocaQrCode">货位二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<object> GetCheckJobMaterialData(string JobQrCode, string LocaQrCode)
        {
            return await _tD_CheckBus.GetCheckJobMaterialDataAsync(JobQrCode, LocaQrCode);
        }



        /// <summary>
        /// PDA盘点扫描单个物料
        /// </summary>
        /// <param name="PackQrCode">物料二维码</param>
        /// <param name="LocaQrCode">货位二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CheckMaterialDTO> GetCheckMaterial(string PackQrCode,string LocaQrCode)
        {
            return await _tD_CheckBus.GetCheckMaterialAsync(PackQrCode, LocaQrCode);
        }

        /// <summary>
        /// 获取盘点的货位信息
        /// </summary>
        /// <param name="CheckQrCode">盘点单二维码</param>
        /// <param name="LocaQrCode">货位二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_Location> GetCheckLocation(string CheckQrCode, string LocaQrCode)
        {
            return await _tD_CheckBus.GetCheckLocationAsync(CheckQrCode, LocaQrCode);
        }

        /// <summary>
        /// PDA盘点列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Check> GetCheckDetailList(PageCheckData data)
        {
            return await _tD_CheckBus.GetCheckDetailListAsync(data);
        }

        /// <summary>
        /// PDA盘点单列表移除
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeleteCheckPackData(CheckDetailDTO data)
        {
            await _tD_CheckBus.DeleteCheckPackDataAsync(data);
        }

        /// <summary>
        /// PDA扫描盘点单扫描货位获取货位上明细物料信息
        /// </summary>
        /// <param name="panId">盘点单id</param>
        /// <param name="locationId">货位id</param>
        /// <param name="MaterName">物料名称</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<CheckMateralInfo>> GetCheckMateralInfo(string panId, string locationId, string MaterName) {
            var res = await _tD_CheckBus.GetCheckMateralInfo(panId, locationId, MaterName);
            return res;
        }
        #endregion

        #region  包重置

        /// <summary>
        /// 获取包二维码信息
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetPackQrCode()
        {
            return await _tD_CheckBus.GetPackQrCodeAsync();
        }

        /// <summary>
        /// 获取成品对应的物料信息
        /// </summary>
        /// <param name="JobQrcode">成品二维码</param>
        /// <param name="PackStatus">false-扫描成品;true-扫描一个成品输入数量</param>
        /// <param name="PackQrCode">包二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PackJobMaterialDTO> GetPackJobMaterialList(string JobQrcode, Boolean? PackStatus, string PackQrCode)
        {
            return await _tD_CheckBus.GetPackJobMaterialListAsync(JobQrcode, PackStatus, PackQrCode);
        }

        /// <summary>
        /// 包重置保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SavePackJobData(PackJobDetailDTO data)
        {
            await _tD_CheckBus.SavePackJobDataAsync(data);
        }

        /// <summary>
        /// 包重置保存(新版)2022-09-06
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SavePackNumsData(PackJobDetailDTO data)
        {
            await _tD_CheckBus.SavePackNumsDataAsync(data, _mongodbOptions);
        }

        #endregion

        #region 调整盘差逻辑调整盘差逻辑2022-8-26(增加盘点库存明细记录)

        /// <summary>
        /// 获取库存信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CheckStockDetailDTO> GetCheckStockDetail(TDcheckDataDTO data)
        {
            return await _tD_CheckBus.GetCheckStockDetailAsync(data);
        }

        /// <summary>
        /// 计算盘差值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<DiscDifferenceDTO> GetDiscDifferenceMaterial(DifferenceDTO data)
        {
            return await _tD_CheckBus.GetDiscDifferenceMaterialAsync(data);
        }

        /// <summary>
        /// 盘差
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public void SaveDifference(DifferenceDTO data)
        {
            _tD_CheckBus.SaveDifference(data);
        }

        /// <summary>
        /// 获取盘点单实际盘点信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<ChcekPackMaterialDTO>> GetChcekPackMaterialList(PageInput<CheckConditionDTO> input)
        {
            return await _tD_CheckBus.GetChcekPackMaterialListAsync(input);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(TD_Check data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);
                data.StorId = _provider.GetRequiredService<IOperator>().Property.DefaultStorageId;
                data.EquId = "1";
                data.IsComplete = false;
                data.Status = 0;

                await _tD_CheckBus.AddDataAsync(data);
            }
            else
            {
                await _tD_CheckBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _tD_CheckBus.DeleteDataAsync(ids);
        }

        #endregion

        #region 查询未完成的单据

        /// <summary>
        /// 查询未完成的备货单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_PickUpMain>> GetCheckPickUpMainList(PageInput<ConditionDTO> input)
        {
            return await _tD_CheckBus.GetCheckPickUpMainListAsync(input);
        }

        /// <summary>
        /// 查询未完成的收货单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<TD_Receiving>> GetCheckReceivingList(PageInput<ConditionDTO> input)
        {
            return await _tD_CheckBus.GetCheckReceivingListAsync(input);
        }

        /// <summary>
        /// 查询未处理的派车单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_TransportBillDTO>> GetCheckTransportBillList(PageInput<ConditionDTO> input)
        {
            return await _tD_CheckBus.GetCheckTransportBillListAsync(input);
        }

        /// <summary>
        /// 查询盘点单是否可以保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Object> GetCheckTrueOrFalse(TD_CheckDTO data)
        {
            return await _tD_CheckBus.GetCheckTrueOrFalseAsync(data);
        }

        #endregion

    }
}